import requests

with open('./douban.txt', 'w') as f:
    page_limit = 50
    page_start = 0
    while True:
        r = requests.get('https://movie.douban.com/j/search_subjects',
                         params={'type': 'movie', 'tag': '热门', 'page_limit': page_limit, 'page_start': page_start},
                         headers={
                             'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) '
                                           'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'})
        if r.status_code == 200:
            movies = r.json()['subjects']
            import json

            print('Request %d movies' % (len(movies)))
            for m in movies:
                f.write(json.dumps(m) + '\n')
            if len(movies) < page_limit:
                break
            else:
                page_start += len(movies)
        else:
            break
